﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Big2.Big2WcfService;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Big2.Big2Game;


namespace Big2.Big2WcfService
{
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
    public class Client : IBig2ServerServiceCallback
    {
        public IBig2ServerService proxy;
        Thread thread;
        public delegate void ClientConnectedEventHandler(List<string> c);
        public event ClientConnectedEventHandler ClientConnected;

        public delegate void ClientDisconnectedEventHandler();
        public event ClientDisconnectedEventHandler ClientDisconnected;

        public Connect Connection { get; set; }


        //delegates
        public delegate void NewTalkMSGEventHandler(string msg);
        public event NewTalkMSGEventHandler NewTalkMSGEventCalled;

        public delegate void PlayerJoinEventHandler(string player);
        public event PlayerJoinEventHandler PlayerJoinEventCalled;

        public delegate void PlayerLeftEventHandler(string player);
        public event PlayerLeftEventHandler PlayerLeftEventCalled;

        public delegate void GameStartedEventHandler(List<string> playersInGame);
        public event GameStartedEventHandler GameStartedEventCalled;

        public delegate void DealHandToPlayerEventHandler(Hand h);
        public event DealHandToPlayerEventHandler DealHandToPlayerEventCalled;

        public delegate void PlayerTurnEventHandler(Play p);
        public event PlayerTurnEventHandler PlayerTurnEventCalled;

        public delegate void GameEndEventHandler(string winnderName, List<Player> players);
        public event GameEndEventHandler GameEndEventCalled;

        public delegate void CurrentPlayEventHandler(Play p);
        public event CurrentPlayEventHandler CurrentPlayEventCalled;

        public Client()
        {

        }

        public void ConnectToServer()
        {
            thread = new Thread(new ThreadStart(DoConnectToServer));
            thread.Start();
        }
        private void DoConnectToServer()
        {
            proxy = new DuplexChannelFactory<IBig2ServerService>(new InstanceContext(this), "NetTcpBinding_IBig2ServerService").CreateChannel();
            List<string> n = proxy.Login(Connection.GetName);            
            if (ClientConnected != null)
            {
                ClientConnected(n);
            }            
        }

        public void DisconnectClient()
        {
            thread = new Thread(DoDisconnectClient);
            thread.Start();
        }
        
        private void DoDisconnectClient()
        {
            try
            {
                if (proxy != null && ((IChannel)proxy).State == CommunicationState.Opened)
                    ((IChannel)proxy).Close();

                if (ClientDisconnected != null)
                    ClientDisconnected();
            }
            catch { };
        }
    

        #region proxy methods

        //public void Talk(string msg)
        //{
        //    proxy.Talk(Connection.GetName,msg);
        //}
        //public int JoinGame(string name)
        //{
        //    return proxy.JoinGame(name);
        //}
        
        #endregion
        

        #region IBig2ServerServiceCallback Members
        public void PLayerLeft(string name)
        {
            if (PlayerLeftEventCalled != null)
                PlayerLeftEventCalled(name);
        }
        public void PlayerJoin(string name)
        {
            if (PlayerJoinEventCalled != null)
                PlayerJoinEventCalled(name);
        }
        public void GameStarted(List<string> playersInGame)
        {
            if (GameStartedEventCalled != null)
                GameStartedEventCalled(playersInGame);
        }
        public void DealHandToPlayer(Hand hand)
        {
            if (DealHandToPlayerEventCalled != null)
                DealHandToPlayerEventCalled(hand);
        }
        public void CurrentPlay(Big2.Big2Game.Play currentPLay)
        {
            if (CurrentPlayEventCalled != null)
                CurrentPlayEventCalled(currentPLay);
        }
        public void PlayerTurn( Play currentPlay)
        {
            if (PlayerTurnEventCalled != null)
                PlayerTurnEventCalled(currentPlay);
        }
        public void GameEnd(string winnderName, List<Player> players)
        {
            if (GameEndEventCalled != null)
                GameEndEventCalled(winnderName,players);
        }
        public void NewTalkMSG(string msg)
        {
            if (NewTalkMSGEventCalled != null)
            {
                NewTalkMSGEventCalled( msg);
            }
        }
        #endregion
    }
}
